#include <stdio.h>	/* fprintf */
#include <stdlib.h>	/* contains exit */
#include <sys/types.h>	/* unistd.h needs this */
#include <unistd.h>	/* contains read/write */
#include <fcntl.h>
#include <malloc.h>
#include <string.h>
#include </usr/include/elf.h>
/*
typedef struct{
	unsigned char e_ident[EI_NIDENT];
	Elf32_Half e_type;
	Elf32_Half e_machine;
	Elf32_Word e_version;
	Elf32_Addr e_entry;
        Elf32_Off  e_phoff;
        Elf32_Off  e_shoff;
        Elf32_Word e_flags;
        Elf32_Half e_ehsize;
        Elf32_Half e_phentsize;
        Elf32_Half e_phnum;
        Elf32_Half e_shentsize;
        Elf32_Half e_shnum;
        Elf32_Half e_shstrndx;
    }Elf32_Ehdr;
typedef struct
{
	Elf32_Word sh_name;  
	Elf32_Word sh_type; 
	Elf32_Word sh_flags; 
	Elf32_Addr sh_addr; 
	Elf32_Off sh_offset; 
	Elf32_Word sh_size; 
	Elf32_Word sh_link; 
	Elf32_Word sh_info; 
	Elf32_Word sh_addralign; 
	Elf32_Word sh_entsize; 
} Elf32_Shdr;
*/
#define MINIX_HEADER 32
#define GCC_HEADER 4096

void die(char * str)
{
	fprintf(stderr,"%s\n",str);
	exit(1);
}

void usage(void)
{
	die("Usage: build boot system [> image]");
}

int main(int argc, char ** argv)
{
	int i,c,id;
	char buf[4096];
	
	Elf32_Ehdr ElfHead;
	Elf32_Ehdr * pElfHead = &ElfHead;

	int lenShdr;
	Elf32_Shdr * pShdr;

	char * pNameTab=0;
	char * pName;

	char * pWritebuf;

	if (argc != 3)
		usage();
	for (i=0;i<sizeof buf; i++) buf[i]=0;
	if ((id=open(argv[1],O_RDONLY,0))<0)
		die("Unable to open 'boot'");
	if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
		die("Unable to read header of 'boot'");
	if (((long *) buf)[0]!=0x04100301)
		die("Non-Minix header of 'boot'");
	if (((long *) buf)[1]!=MINIX_HEADER)
		die("Non-Minix header of 'boot'");
	if (((long *) buf)[3]!=0)
		die("Illegal data segment in 'boot'");
	if (((long *) buf)[4]!=0)
		die("Illegal bss in 'boot'");
	if (((long *) buf)[5] != 0)
		die("Non-Minix header of 'boot'");
	if (((long *) buf)[7] != 0)
		die("Illegal symbol table in 'boot'");
	i=read(id,buf,sizeof buf);
	fprintf(stderr,"Boot sector %d bytes.\n",i);
	if (i>510)
		die("Boot block may not exceed 510 bytes");
	buf[510]=0x55;
	buf[511]=0xAA;
	i=write(1,buf,512);
	if (i!=512)
		die("Write call failed");
	close (id);
	
	if ((id=open(argv[2],O_RDONLY,0))<0)
		die("Unable to open 'system'");
	if (read(id,buf,GCC_HEADER) != GCC_HEADER)
		die("Unable to read header of 'system'");
	if (((long *) buf)[6] != 0)
		die("Non-GCC header of 'system'");
	
	ElfHead = *((Elf32_Ehdr * )buf);
	fprintf(stderr, "%s", "ELF Header:\n");
	fprintf(stderr, "%s","Magic:");
	for(i=0; i<EI_NIDENT; i++)
	{
		fprintf(stderr, " 0x%x", pElfHead->e_ident[i]);
	}
	fprintf(stderr, "%s", "\n");
	fprintf(stderr, "Entry Point:\t0x%x\n", pElfHead->e_entry);
	fprintf(stderr, "S Header offet:\t0x%x\n", pElfHead->e_shoff);	
	fprintf(stderr, "S Header size:\t0x%x\n", pElfHead->e_shentsize);
	fprintf(stderr, "S Header num:\t0x%x\n", pElfHead->e_shnum);
	fprintf(stderr, "S Index:\t0x%x\n", pElfHead->e_shstrndx);

	lenShdr = pElfHead->e_shnum * pElfHead->e_shentsize;
	pShdr = (Elf32_Shdr *)malloc(lenShdr);
	if(lseek(id,pElfHead->e_shoff,SEEK_SET) >= 0)
	{
		c = read(id, pShdr, lenShdr);
	}
	else
	{
		die("Read Sesstion header failed!\n");
	}

	for(i=0; i<pElfHead->e_shnum; i++)
        {
		if(((pShdr[i]).sh_type) == SHT_STRTAB)
		{
			pNameTab = (char *)malloc((pShdr[i]).sh_size);
			if(lseek(id,(pShdr[i]).sh_offset,SEEK_SET) >= 0)
		 	{
				c = read(id, pNameTab, (pShdr[i]).sh_size);
			}
			else
			{
				die("Read Sesstion header failed!\n");
			}
			break;
		}
        }

	if(pNameTab == 0)
	{
		die("No SHT_STRTAB found!\n");
	}

	for(i=0; i<pElfHead->e_shnum; i++)
	{
		pName = &(pNameTab[(pShdr[i]).sh_name]);
		fprintf(stderr,"[%d]",i);
		fprintf(stderr,"\t0x%x",(pShdr[i]).sh_type);
		fprintf(stderr,"\t0x%x",(pShdr[i]).sh_offset);
		fprintf(stderr,"\t0x%x",(pShdr[i]).sh_addr);
		fprintf(stderr,"\t0x%x",(pShdr[i]).sh_size);
		fprintf(stderr,"\t%s",pName);
		fprintf(stderr,"%s","\n");

		if(((strcmp(pName, ".data")==0) || (strcmp(pName, ".text")==0)) || (strcmp(pName, ".rodata") == 0))
                {
                        pWritebuf = (char *)malloc((pShdr[i]).sh_size);
                        if((lseek(id,(pShdr[i]).sh_offset,SEEK_SET) >= 0)
				&&(lseek(1,(pShdr[i]).sh_addr+512,SEEK_SET) >= 0))
                        {
                                if((c=read(id, pWritebuf, (pShdr[i]).sh_size)) > 0)
				{
					c = write(1,pWritebuf,c);
					fprintf(stderr,"Write System %s %d bytes.\n",pName, c);
				}
			}
			else
			{
				die("Seek Sesstion data failed!\n");
			}
		}
	}

	close(id);
	close(1);
	return(0);
}
